
<?php $this->beginClip('pagetitle'); ?>明细<?php $this->endClip(); ?>
<div class="am-g" style="padding: 10px">

    <div style="width: 75%;position: fixed;left: 12%;background-color: rgba(234,233,254,0.5);">

        <div class="am-alert am-alert-danger" id="my-alert" style="display: none">
            <p>开始日期应小于结束日期！</p>
        </div>
        <div class="am-g">
            <div class="am-u-sm-5">
                <button type="button" class="am-btn am-btn-default am-margin-right" name="start_time" id="my-start">开始日期</button><span id="my-startDate"></span>
            </div>
            <div class="am-u-sm-5">
                <button type="button" class="am-btn am-btn-default am-margin-right" name="end_time" id="my-end">结束日期</button><span id="my-endDate"></span>
            </div>
            <div class="am-u-sm-2">
                <button type="button" class="am-btn am-btn-primary" id="my-search">搜索</button>
            </div>
        </div>
    </div>

    <table class="am-table am-table-bordered am-table-radius am-table-striped am-table-hover" style="margin-top: 5%">
        <thead>
        <tr>
            <th>代理商名称</th>
            <th>门店名称</th>
            <th>红包数</th>
            <th>交易额</th>
            <th>交易笔数</th>
            <th>创建时间</th>
        </tr>
        </thead>
        <tbody id="detail">
            <?php foreach ($detail['merchents'] as $k=>$merchant):?>
                <tr>
<!--                    --><?php //if($k==0):?>
                        <td "<?php echo count($detail['merchents'])?>"><?= $detail['name']?></td>
<!--                    --><?//endif;?>
                    <td><?= $merchant['name']?></td>
                    <td><?= round($merchant['trade_data'][0]['discount_sum']/100,2)?></td>
                    <td><?= round($merchant['trade_data'][0]['amount_due_sum']/100,2)?></td>
                    <td><?= $merchant['trade_data'][0]['trade_sum']?></td>
                    <td><?= $merchant['createtime']?></td>

                </tr>
            <?php endforeach;?>
        </tbody>
    </table>
</div>

<script>
    $(function() {
        var nowTemp = new Date();
        var nowDay = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0).valueOf();
        var yestoDay = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate()-1, 0, 0, 0, 0).valueOf();
        var nowMoth = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), 1, 0, 0, 0, 0).valueOf();
        var nowYear = new Date(nowTemp.getFullYear(), 0, 1, 0, 0, 0, 0).valueOf();

        var startDate = new Date(nowYear, nowMoth, yestoDay);
        var endDate = new Date(nowMoth, nowMoth, nowDay);
        var $alert = $('#my-alert');
        var flag = false;
        $('#my-start').datepicker().
        on('changeDate.datepicker.amui', function(event) {
            if (event.date.valueOf() > endDate.valueOf()) {
                $alert.find('p').text('开始日期应小于结束日期！').end().show();
                flag = false;
            } else {
                $alert.hide();
                startDate = new Date(event.date);
                $('#my-startDate').text($('#my-start').data('date'));

            }
            $(this).datepicker('close');
        });

        $('#my-end').datepicker().
        on('changeDate.datepicker.amui', function(event) {
            if (event.date.valueOf() < startDate.valueOf()) {
                $alert.find('p').text('结束日期应大于开始日期！').end().show();
                flag = false;
            } else {
                $alert.hide();
                endDate = new Date(event.date);
                $('#my-endDate').text($('#my-end').data('date'));
                flag = true;
            }
            $(this).datepicker('close');
        });

        $('#my-search').click(function(){
            if(flag){
                $.ajax({
                    url:'/tool/DetailAjax',
                    type:'post',
                    data:{'start_time':$('#my-start').data('date'),'end_time':$('#my-end').data('date'),'agent_id':<?= $detail['id_agent']?>},
                    success:function(res){
                        console.log(res);
                        $('#detail').html(res);
                    }
                });
            }
        });
    });
</script>